package com.loiane.com.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

/**
 * City POJO class.
 * Represents the City table
 * 
 * @author Loiane Groner
 * http://loianegroner.com (English)
 * http://loiane.com (Portuguese)
 */
@Entity
@Table(name="CITY")
public class City {

	private long id;
	
	private String name;
  
	private List<City> childs;
	private City parent;
	
	

	public City(String name) {
		super();
		this.name = name;
	}

	public City() {}

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="CITY_ID")
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	@Column(name="CITY_NAME", nullable=false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(mappedBy="parent",fetch=FetchType.LAZY)
	@Cascade(value={CascadeType.ALL})
	public List<City> getChilds() {
		return childs;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="Parent")
	public City getParent() {
		return parent;
	}

	public void setParent(City parent) {
		this.parent = parent;
	}
  
	public void setChilds(List<City> childs) {
		this.childs = childs;
		for(City c:childs)c.setParent(this);
	}

	
	
}
